#ifndef __ACCEPTOR_H__
#define __ACCEPTOR_H__

#include "Spark/Net/Channel.h"
#include "Spark/Net/Socket.h"

using NewConnectionCallback = std::function<void(int, const SocketAddr&)>;
class EventLoop;
class SocketAddr;

class Acceptor : public NonCopyable
{
  public:
    Acceptor(EventLoop* loop, const SocketAddr& addr);
    ~Acceptor();

  public:
    void listen();
    bool listening() const
    {
        return isListening_;
    }
    const SocketAddr& addr() const
    {
        return addr_;
    }
    void setNewConnectionCallback(NewConnectionCallback cb)
    {
        newConnectionCallback_ = std::move(cb);
    }

  protected:
    void handleRead();

    EventLoop* loop_;
    Socket socket_;
    SocketAddr addr_;
    NewConnectionCallback newConnectionCallback_;
    Channel acceptorChannel_;
    int idleFD_;
    bool isListening_{false};
};

#endif  // __ACCEPTOR_H__